﻿using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;

namespace WMLogistics_Dao
{
    public class CompanyApplyDao
    {
         private SqlGeneratorImpl sqlGenerator;
         public CompanyApplyDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

         public PageData<wuliu_wlcompany_business> GetList(IPredicate predicate, int page, int pageSize, int company_id)
         {
             var data = new PageData<wuliu_wlcompany_business>();
             IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_wlcompany_business>(p => p.id,false)
                                    };
             var sql = "select * from wuliu_wlcompany_business where state=1 and wuliu_id not in (select wuliu_id FROM wuliu_wlcompany_apply where apply_month='" + DateTime.Now.ToString("yyyyMM") + "' and company_id=" + company_id + ") and wuliu_id not in (select wuliu_id from wuliu_contract_base where company_id=" + company_id + ") ";
             //and wuliu_id not in (select wuliu_id from wuliu_contract_base where contract_flag=1 where company_id=@company_id)
             using (var conn = ConnectionHelper.OpenConnection())
             {
                 var pageDate = new PageData<wuliu_wlcompany_business>();
                 DynamicParameters dynamicParameters = new DynamicParameters();
                 var whereSql = GetWhereSql(predicate, dynamicParameters);
                 if (!string.IsNullOrEmpty(whereSql))
                     sql += " and " + whereSql;
                 var count = conn.Query<wuliu_wlcompany_business>(sql, dynamicParameters).Count();
                 sql += "order by wuliu_wlcompany_business.id asc limit @pageIndex,@pageSize";
                 dynamicParameters.Add("pageIndex", page*pageSize);
                 dynamicParameters.Add("pageSize", pageSize);
                 //var count = conn.Count<wuliu_bid_load>(predicate);
                 data.Items = conn.Query<wuliu_wlcompany_business>(sql, dynamicParameters).ToList();
                 data.TotalNum = count;
                 data.CurrentPage = page;
                 data.TotalPageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
             }
             return data;

             //IList<ISort> sorts = new List<ISort>() { 
             //    Predicates.Sort<wuliu_wlcompany_business>(p => p.id,false)
             //};
             //using (var conn = ConnectionHelper.OpenConnection())
             //{
             //    var pageData = new PageData<wuliu_wlcompany_business>();
             //    var count = conn.GetList<wuliu_wlcompany_business>(predicate).Count();
             //    pageData.Items = conn.GetPage<wuliu_wlcompany_business>(predicate, sorts, page, pageSize).ToList();
             //    pageData.TotalNum = count;
             //    pageData.CurrentPage = page;
             //    pageData.TotalPageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
             //    return pageData;
             //}
         }

         private string GetWhereSql(IPredicate predicate, DynamicParameters dynamicParameters)
         {
             Dictionary<string, object> parameters = new Dictionary<string, object>();
             var wherSql = predicate.GetSql(sqlGenerator, parameters);

             foreach (var parameter in parameters)
             {
                 dynamicParameters.Add(parameter.Key, parameter.Value);
             }
             return wherSql;
         }
    }
}
